package 周赛;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class 统计追加字母可以获得的单词数 {

    public int wordCount(String[] startWords, String[] targetWords) {
        Set<Integer> startSet = new HashSet<>();
        for (String str : startWords) {
            startSet.add(toInt(str));
        }

        int ans = 0;
        for (String target : targetWords) {
            for (int i = 0; i < target.length(); i++) {
                //尝试将目标单词删除一个单词，看是否在 startWords 中存在
                int t = toInt(target, i);
                if (startSet.contains(t)) {
                    ans++;
                    break;
                }
            }
        }
        return ans;
    }
    // 忽略指定位置处，然后将单词转换成对应的int
    public int toInt(String word, int ignore) {
        int num = 0;
        for (int i = 0; i < word.length(); i++) {
            if (i == ignore) continue;
            int bit = word.charAt(i) - 'a';
            num += 1 << bit;
        }

        return num;
    }

    // 将单词转换成对应的int
    public int toInt(String word) {
        int num = 0;
        for (char c : word.toCharArray()) {
            int bit = c - 'a';
            num += 1 << bit;
        }

        return num;
    }
}
